package dev.view;

import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import dev.controller.myControllerImpl;

public class myViewImpl {
	private myControllerImpl control;
	
	public myViewImpl(myControllerImpl inControl){
		control = inControl;
		anmelden();
	}
	
	public void print(String inString){
		System.out.println(inString); // :)
	}
	
	public void anmelden(){
		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
		
		print("Login");
		String benutzerName = null;
		String password = null;
		
		do{
			try{
				System.out.print("Benutzername:\t");
				benutzerName = input.readLine();
				System.out.print("Password:\t");
				password = input.readLine();
			}catch (Exception e){}
		}while(!control.anmelden(benutzerName,password));
		UserroleInterface[] roleContainer = control.get_userrole();
		int index = 0;
		if(roleContainer.length > 1){
			print("Menu");
			for(int i=0;i<roleContainer.length;++i){
				print((i+1) + " " + roleContainer[i].getName());
			}
			boolean ok;
			int menuWahl;
			do{
				menuWahl = 0;
				do{
					ok = true;
					try{
						System.out.print("Rolle:\t");
						menuWahl = Integer.parseInt(input.readLine());
					}catch(Exception e){
						ok = false;
						print("Falsche Eingabe!");
						Toolkit.getDefaultToolkit().beep();
					}
				}while(!ok);
				if(menuWahl < 1 || menuWahl > roleContainer.length){
					ok = false;
					print("Falsche Eingabe!");
					Toolkit.getDefaultToolkit().beep();
				}
			}while(!ok);
			index = menuWahl-1;
		}
		print("Aktive Rolle: "+roleContainer[index].getName());
		roleContainer[index].showMenu(control);
	}
}
